import { createRouter, createWebHistory } from 'vue-router';
//createRouter方法，用于创建路由器实例，可以管理多个路由
import Layout from '@/layout/index.vue';

export const constantRoutes = [
  {
    path: '/',
    component: Layout,
    children: [
      {
        path: '',
        component: () => import('@/views/home/index.vue'),
        meta: { title: '首页' }
      },
      {
        path: '/home/goods/category/:id',
        component: () => import('@/views/goods/category/index.vue'),
        meta: { title: '商品分类' }
      },
      {
        path: '/home/goods/detail/:id',
        component: () => import('@/views/goods/details/index.vue'),
        meta: { title: '商品详情' }
      },
      {
        path: '/user/cart',
        component: () => import('@/views/carts/index.vue'),
        meta: { title: '购物车' }
      },
      {
        path: '/user/order/before/:params',
        component: () => import('@/views/order/index.vue'),
        meta: { title: '创建订单' }
      },
      {
        path: '/user/order/pay/:params',
        component: () => import('@/views/order/pay.vue'),
        meta: { title: '订单支付' }
      },
      {
        path: '/user',
        component: () => import('@/views/users/index.vue'),
        meta: { title: '用户' },
        children: [
          {
            path: '/user/address',
            component: () => import('@/views/users/address/index.vue'),
            meta: { title: '收货地址' }
          }
        ]
      }
    ]
  },
  {
    path: '/home/login',
    component: () => import('@/views/login/login.vue'),
    meta: { title: '登录' }
  },
  {
    path: '/home/register',
    component: () => import('@/views/login/register.vue'),
    meta: { title: '注册' }
  },
  {
    path: '/home/forget_password',
    component: () => import('@/views/login/forget_password.vue'),
    meta: { title: '忘记密码' }
  },
  {
    path: '/index',
    component: () => import('@/views/home/index.vue'),
    meta: { title: '首页' }
  }
];

const router = createRouter({
  history: createWebHistory(import.meta.env.VITE_APP_CONTEXT_PATH),
  routes: constantRoutes,
  //滚动行为：控制滚动条
  scrollBehavior() {
    return {
      left: 0,
      top: 0
    };
  }
});

export default router;
